<?php
    require('plugin.php');

    if (!isset($_GET['id'])){
        header("HTTP/1.1 404 Not Found");
        exit('invalid parameter:id');
    }

    $mserver=new mserver();

    $fileurl=$mserver->getInfo($_GET['id'])->songLink;
        
        $ch=curl_init();

        curl_setopt($ch, CURLOPT_URL, $fileurl);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_REFERER, "http://play.baidu.com");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        // 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文
        curl_setopt($ch, CURLOPT_HEADER, true);
        // 是否不需要响应的正文,为了节省带宽及时间,在只需要响应头的情况下可以不要正文
        curl_setopt($ch, CURLOPT_NOBODY, false);
        

        if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])){
            // 有range
            curl_setopt($ch,CURLOPT_RANGE,str_replace('bytes=','',$_SERVER['HTTP_RANGE']));
        }


        $file=curl_exec($ch);

        $head=substr($file,0,curl_getinfo($ch,CURLINFO_HEADER_SIZE));
        $body=substr($file,curl_getinfo($ch,CURLINFO_HEADER_SIZE));

        curl_close($ch);

        $headArr = explode(';',str_replace(array("\r\n", "\r", "\n"), ";", $head));

        foreach($headArr as $value)
        {
            if (strpos($value,'filename'))
            {
                continue;
            }

            if (strpos($value,'attachment'))
            {
                continue;
            }

            header($value);
            
        }

        echo $body;

?>